import AudioPlayer from "../framework/core/AudioPlayer";
import MathUtil from "../framework/utils/MathUtil";

 
/**
 * 游戏逻辑工具
 */
 
export default class Tools{

    static playMusic(url){
        cc.loader.loadRes(url, cc.AudioClip,(err,audio)=>{  
            AudioPlayer.playMusic(audio);
        }) 
    } 
    static playEffect(url,isLoop =false){
        cc.loader.loadRes(url, cc.AudioClip,(err,audio)=>{  
            AudioPlayer.playEffect(audio,isLoop);
        }) 
    } 
    /**
     * 获取当前系统时间
     * @param isMs 是否毫秒
     */
    static getTime(isMs?:boolean){
        if(isMs)return Math.floor(Date.now());
        return Math.floor(Date.now() /1000);
    }

     //保留小数点后几位
    static  toFixed(number,fractionDigits){
        var times = Math.pow(10, fractionDigits);
        var roundNum = Math.round(number * times) / times;
        return roundNum.toFixed(fractionDigits);
    }

    //是否为空
    static isEmpty = (str: any): boolean => {
        if (
          str === null ||
              str === '' ||
              str === undefined ||
              str.length === 0
        ) {
          return true
        } else {
          return false
        }
    }; 
      
    // 跨天检查
    static checkOtherDay(t1, t2, rt) {
        if (Tools.isEmpty(t1)) {
            return false;
        }
        t2 = t2 || 0;
        rt = rt || 0;

        if (Math.abs(t2 - t1) > 86400) {
            return true;
        }
        return t2 > Tools.figureResetTime(t1, rt);
    }

    // 计算重置点
    static  figureResetTime(t1, rt) {
        var rH = Math.floor(rt / 60 / 60);
        var rM = Math.floor(rt / 60 % 60);
        var rS = Math.floor(rt % 60);
        var dd = new Date(2099, 1, 1, rH, rM, rS);
        var rd = dd.getTime() / 1000;
        return t1 + ((rd - t1) % 86400);
    }

    //button置灰节点
   static grayButton(btnNode,isEnable){
        if(isEnable){
           btnNode.interactable = false; 
           btnNode.enableAutoGrayEffect = true;
        }else{
            btnNode.interactable = true; 
            btnNode.enableAutoGrayEffect = false;
        }
    }

    //获取发布后的资源路径
    static getUrl(url){
        url = cc.url.raw(url);
        if(cc.loader.md5Pipe){
            url = cc.loader.md5Pipe.transformURL(url);
        }

        try {
            //@ts-ignore
            wx.getFileSystemManager.accessSync(url)
        } catch (error) {
            url=(<any>window).wxDownloader.REMOTE_SERVER_ROOT + "/" +url;
        }

        return url;
    }

    /**
     * 
     * @param aniName 重置懂初始化状态
     */
    static resetAni(aniObj, aniName?:string){
        let ani:cc.Animation = aniObj;
        let curName:string = aniName ||    ani.currentClip.name ;
        ani.play(curName,0);
        ani.sample(curName)
        ani.stop();
    } 


    /** 把 node1移动到 node2的位置 */
    static moveN1toN2(node1, node2) {
        node1.position = node1.parent.convertToNodeSpaceAR(node2.parent.convertToWorldSpaceAR(node2.position))
    }

    /** 获取把 node1移动到 node2位置后的坐标 */
    static convertNodeSpaceAR(node1, node2) {
        return node1.parent.convertToNodeSpaceAR(node2.parent.convertToWorldSpaceAR(node2.position))
    }

    /**
     * 对比版本号
     * @param v1 1.0.2
     * @param v2 1.0.3
     */
    static compareVersion(v1, v2) {
        v1 = v1.split('.');
        v2 = v2.split('.');
        var len = Math.max(v1.length, v2.length);
        while (v1.length < len) {
            v1.push('0')
        }
        while (v2.length < len) {
            v2.push('0')
        }
        for (var i = 0; i < len; i++) {
            var num1 = parseInt(v1[i]);
            var num2 = parseInt(v2[i]);
            if (num1 > num2) {
                return 1
            } else if (num1 < num2) {
                return -1
            }
        }
        return 0
    }

   static getRanomList(){
        let arr = []
        while (arr.length < 4) {
            arr.push(MathUtil.getRandomInt(1,539)) 
        }
        return arr;
    }


    static dateFormat(fmt = 'YYYY-mm-dd HH:MM:SS', date = new Date()) {
        let ret
        const opt = {
          'Y+': date.getFullYear().toString(), // 年
          'm+': (date.getMonth() + 1).toString(), // 月
          'd+': date.getDate().toString(), // 日
          'H+': date.getHours().toString(), // 时
          'M+': date.getMinutes().toString(), // 分
          'S+': date.getSeconds().toString() // 秒
        }
        for (const k in opt) {
          ret = new RegExp('(' + k + ')').exec(fmt)
          if (ret) {
            fmt = fmt.replace(ret[1], (ret[1].length === 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, '0')))
          }
        }
        return fmt
      } 
    
    /**
     * 获取用户名字
     */
    static getPlayerName(name:string){
        if(!name){
            return "";
        } 
        if(name.length > 6){
            return name.substring(0,6) +"...";
        }

        return name;
    }
 
}



window["Tools"] = Tools;
